package org.jeecg.modules.oms.parser.impl.confirmation;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.oms.entity.ProductSalesOrderDetail;
import org.jeecg.modules.oms.parser.WindowConfirmationFileParser;
import org.jeecg.modules.system.entity.SysEmployee;
import org.jeecg.modules.system.service.ISysEmployeeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 新格尔窗户/门确认单分发器
 * 根据用户的门窗类型设置，分发到对应的解析器
 */
@Component
public class XinGeErWindowDoorDispatcher implements WindowConfirmationFileParser {

    private static final Logger log = LoggerFactory.getLogger(XinGeErWindowDoorDispatcher.class);

    @Autowired
    private XinGeErWindowConfirmationFileParser windowParser;

    @Autowired
    private XinGeErDoorConfirmationFileParser doorParser;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Autowired
    private ISysEmployeeService sysEmployeeService;

    /**
     * 根据用户门窗类型分发解析请求
     *
     * @param file     Excel文件
     * @param tenantId 租户ID
     * @return 产品销售订单明细列表
     * @throws IOException 文件读取异常
     * @throws Exception   其他解析异常
     */
    @Override
    public List<ProductSalesOrderDetail> parseFile(File file, Integer tenantId) throws IOException, Exception {
        // 获取当前用户的门窗类型
        Integer windowDoorType = getCurrentUserWindowDoorType();
        
        log.info("用户门窗类型: {}, 文件: {}", windowDoorType, file.getName());
        
        if (windowDoorType != null && windowDoorType == 1) {
            // 门类型，使用门确认单解析器
            log.info("使用门确认单解析器处理文件");
            return doorParser.parseFile(file, tenantId);
        } else {
            // 窗户类型或未设置，使用原有窗户确认单解析器
            log.info("使用窗户确认单解析器处理文件");
            return windowParser.parseFile(file, tenantId);
        }
    }

    /**
     * 获取当前用户的门窗类型
     */
    private Integer getCurrentUserWindowDoorType() {
        try {
            // 获取当前请求
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (attributes == null) {
                log.warn("无法获取当前请求上下文");
                return null;
            }
            
            HttpServletRequest request = attributes.getRequest();
            
            // 从请求中获取token
            String token = request.getHeader("X-Access-Token");
            if (token == null || token.isEmpty()) {
                log.warn("请求中未找到访问token");
                return null;
            }
            
            // 通过token获取登录用户信息
            LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if (loginUser == null) {
                log.warn("无法通过token获取用户信息");
                return null;
            }
            
            // 查询员工信息获取门窗类型
            SysEmployee employee = sysEmployeeService.getById(loginUser.getId());
            if (employee == null) {
                log.warn("未找到员工信息，用户ID: {}", loginUser.getId());
                return null;
            }
            
            Integer windowDoorType = employee.getWindowDoorType();
            log.debug("获取到用户门窗类型: {}", windowDoorType);
            
            return windowDoorType;
            
        } catch (Exception e) {
            log.error("获取用户门窗类型失败", e);
            return null; // 默认使用窗户解析器
        }
    }
}
